package com.weihua.util;


import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class ScheduledUtil {
    private static ScheduledExecutorService scheduledExecutorService;

    public ScheduledUtil() {
    }

    public static ScheduledFuture<?> schedule(Runnable runnable, long milliseconds) {
        return scheduledExecutorService.schedule(runnable, milliseconds, TimeUnit.MILLISECONDS);
    }

    public static ScheduledFuture<?> scheduleAtFixedRate(Runnable runnable, long milliseconds) {
        return scheduledExecutorService.scheduleAtFixedRate(runnable, milliseconds, milliseconds, TimeUnit.MILLISECONDS);
    }

    public static ScheduledFuture<?> scheduleWithFixedDelay(Runnable runnable, long milliseconds) {
        return scheduledExecutorService.scheduleWithFixedDelay(runnable, milliseconds, milliseconds, TimeUnit.MILLISECONDS);
    }

    static {
        scheduledExecutorService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() * 2 - 1);
    }

}
